package com.kkb.mybatis.framework.builder;

import com.kkb.mybatis.framework.config.Configuration;
import com.kkb.mybatis.framework.factory.DefaultSqlSessionFactory;
import com.kkb.mybatis.framework.factory.SqlSessionFactory;
import com.kkb.mybatis.utils.DocumentUtils;
import org.dom4j.Document;

import java.io.InputStream;

public class SqlSessionFactoryBuilder {

    public SqlSessionFactory build(InputStream inputStream){
        //获取Configuration(XMLConfigBuilder)
        Configuration configuration = new Configuration();
        Document document = DocumentUtils.getDocument(inputStream);
        XMLConfigBuilder xmlConfigBuilder = new XMLConfigBuilder(configuration);
        xmlConfigBuilder.parseConfiguration(document.getRootElement());
        //获取SqlSessionFactory
        return  build(configuration);
    }


    public SqlSessionFactory build(Configuration configuration) {
        return new DefaultSqlSessionFactory(configuration);
    }
}
